<? 
session_start();
require_once "php/connect.php";
require_once "php/functions.php";
require_once('php/uploadify/class.upload.php');


if(isset($_SESSION['email']) && isset($_SESSION['password']) && isset($_SESSION['userId'])){
	$userId =  mysql_real_escape_string($_SESSION['userId']);
	$userName =  mysql_real_escape_string($_SESSION['userName']);
	$abfrage = "SELECT user.userId
			   ,user.picture
			   ,user.userName
			   ,user.avatar
			   ,user.posts
			   ,user.icq
			   ,user.email
			   ,user.msn
			   ,user.cdate
			   ,user.gameCharacter
			   ,user.gameServer
			   ,user.notification
		FROM 
		ffxivc_users as user
		WHERE user.userId = '".$userId."' ";

	$res = mysql_query($abfrage);
	$num = mysql_num_rows($res);
	
	if ($num = 1){
		$foundUser = true;
		$av = mysql_result($res, 0, "avatar");
		$pic = mysql_result($res, 0, "picture");
		$name = mysql_result($res, 0, "userName");
		$cd = mysql_result($res, 0, "cdate");
		$pos = mysql_result($res, 0, "posts");
		$char = mysql_result($res, 0, "gameCharacter");
		$server = mysql_result($res, 0, "gameServer");
		$icq = mysql_result($res, 0, "icq");
		$msn = mysql_result($res, 0, "msn");
		$email = mysql_result($res, 0, "email");
		$notification = mysql_result($res, 0, "notification");
	}
	$newMsg = getNewMessages();
	if($newMsg == 0){
		$newMsg = "";
	}else{
		$newMsg = " (".$newMsg.")";
	}
	$output = "
	<html>
		<head>	 	
			<!-- Register -->
		</head>
		<body>
			<script type='text/javascript'>
				$(document).ready(function(){
					$('.show-user').click(function(e) {	
						var clicked = jQuery(e.target);
						var id = clicked.attr('id');
						getUser(id);
					});
				});
			
				function resetPicture(){
					showUpload(0);
					$('#upload_popup').load('php/uploadify/showPicture.php');
					loadWindow();
				}
				function resetAvatar(){
					showUpload(0);
					$('#upload_popup').load('php/uploadify/showAvatar.php');
				}
					
				$(document).ready(function(){
					resetPicture();
					$(function() {
						$('#upload_popup').draggable({cancel: '#upload_popup_body'});
					});
				});
				
				function showPicture(){
					$('#upload_popup').load('php/uploadify/showPicture.php');
					showUpload(1);
				}
				function showAvatar(){
					$('#upload_popup').load('php/uploadify/showAvatar.php');
					showUpload(1);
				}
				
				function showUpload(toggle) {
					if(toggle=='1'){
						$('#upload_popup').fadeIn('fast');
					}else{
						$('#upload_popup').fadeOut('fast');
					}
				};
				function saveProfile(){
					$.post('php/saveProfile.php', {name: $('#name').val(), email: $('#email').val(), character: $('#character').val(), server: $('#server').val(), notification: $('#notification').attr('checked') },
						function(data){
							if(data.status == 'success'){
								$('#status_message').html('Profile saved')
							}else{
								$('#status_message').html('An error occured')
							}
							
						}, 'json');
				
				}
				
				
			</script>
			<div id='upload_popup'>
			</div>
			
			<div class = 'content-title-container'>
				<div id='".$userName."' class='show-user cur-point normal-text blue-text content-title-text fl-left' >".$userName."</div><div class='normal-text grey-text content-title-text fl-left' style='margin-left: 5px;'> 's profile</div>
				<ul id='sub-menu-item-list'><li class='menu-item bold-text'>Specs</li><li class='menu-item bold-text menu-item-selected'>Profile</li><li class='menu-item bold-text'>Messages".$newMsg."</li></ul>
				<script>
					enableMenu();
				</script>
			</div>
			<div id='indent-content-container' class='indent-content-container'>
				<form method='POST' action='javascript:saveProfile();'>
					<table>
						<tr class='tr-table border-bottom'>
							<td class='td-table vertical-middle width-180'><div class='normal-text bold-text'>Name:</div></td>
							<td class='td-table vertical-middle width-260 pad-tb-5'><input id='name' type='text'  size='36' value='".$name."'></td>
							<td class='td-table vertical-middle'><div id='username_err' class='normal-text bold-text marg-l error-container width-220'></div></td>
						</tr>
						<tr class='tr-table border-bottom'>
							<td class='td-table vertical-middle'><div class='normal-text bold-text'>Email:</div></td>
							<td class='td-table vertical-middle pad-tb-5'><input id='email' type='text' size='36' value='".$email."'/></td>
							<td class='td-table vertical-middle'><div id='email_err' class='normal-text bold-text marg-l error-container'></div></td>
						</tr>
						<tr class='tr-table border-bottom'>
							<td class='td-table vertical-middle'><div class='normal-text bold-text'>Notification:</div></td>
							<td class='td-table vertical-middle pad-tb-5'><input id='notification' type='checkbox' value='1'";
			if($notification == 'Y'){
				$output .= "checked";
			}
			$output .=		"/></td>
							<td class='td-table vertical-middle'><div id='email_err' class='normal-text bold-text marg-l error-container'></div></td>
						</tr>
						<tr class='tr-table border-bottom'>
							<td class='td-table vertical-middle'><div class='normal-text bold-text'>Character:</div></td>
							<td class='td-table vertical-middle pad-tb-5'><input id='character' type='text'size='36' value='".$char."'/></td>
							<td class='td-table vertical-middle'><div id='password_err' class='normal-text bold-text marg-l error-container'></div></td>
						</tr>
						<tr class='tr-table border-bottom'>
							<td class='td-table vertical-middle'><div class='normal-text bold-text'>Server:</div></td>
							<td class='td-table vertical-middle pad-tb-5'><input id='server' type='text'size='36' value='".$server."'/></td>
							<td class='td-table vertical-middle'><div id='password_err' class='normal-text bold-text marg-l error-container'></div></td>
						</tr>
						<tr class='tr-table border-bottom'>
							<td class='td-table vertical-middle '>
								<div class='normal-text bold-text'>Avatar:</div>
								<div><a class='font-10' href=\"javascript:showAvatar();\">Change Avatar</a></div>
							</td>";
						if($av != ""){
							$output .= "<td class='td-table vertical-middle pad-tb-5'><div class='border-av fl-left'><div id='preview_av_container'><img id='preview_av' class='avatar-pic' src='".$av."?".time()."'/></div></div>";
						}else{
							$output .= "<td class='td-table vertical-middle pad-tb-5'><div class='border-av fl-left'><div id='preview_av_container'><img id='preview_av' class='avatar-pic' src='media/no-avatar.png'/></div></div>";
						}
						$output .= "
								<!--<div style='padding-top: 29px;'><a class='ver-bot' href=\"javascript:showAvatar();\">Change Avatar</a></div>-->
							</td>
							<td class='td-table vertical-middle'><div id='password_err' class='normal-text bold-text marg-l error-container width-240'></div></td>
						</tr>
						<tr class='tr-table border-bottom'>
							<td class='td-table vertical-middle'>
								<div class='normal-text bold-text'>Picture:</div>
								<div ><a class='font-10' href=\"javascript:showPicture();\">Change Picture</a></div>
							</td>";
						if($pic != ""){
							$output .= "<td class='td-table vertical-middle pad-tb-5'><div class='border-pic fl-left'><img id='profile_pic' src='".$pic."?".time()."' class='marg-auto'/></div>";
						}else{
							$output .= "<td class='td-table vertical-middle pad-tb-5'><div class='border-pic' fl-left><img id='profile_pic' src='media/unknown_n.png' class='marg-auto'/></div>";
						}
						$output .= "
							</td>
							<td class='td-table vertical-middle'><div id='password_err' class='normal-text bold-text marg-l error-container'></div></td>
						</tr>
						<tr class='tr-table'>
							<td class='td-table vertical-middle pad-tb-5' colspan='2'><div id='status_message' class='normal-text bold-text error-container'></div></td>
							<td class='td-table vertical-middle horizontal-right' ><div id='save-submit-button'><input id='save-button' type='submit' value='Save'/></div></td>
						</tr>
					</table>
					<div class></div>
				</form>
			</div>
		</body>
	</html>";
}
echo $output;

?>
